//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

use git2::{Error, Repository};
use serde::Serialize;
use std::path::PathBuf;

#[derive(Serialize)]
struct StashInfo {
    commit_id: String,
    commit_summary: String,
}

pub fn exec(git_path: PathBuf) -> Result<(), Error> {
    let repo = Repository::open(git_path);
    if repo.is_err() {
        return Err(repo.err().unwrap());
    }
    let mut repo = repo.unwrap();

    let mut info_list = Vec::new();

    let res = repo.stash_foreach(|_, msg, commit_id| {
        info_list.push(StashInfo{
            commit_id: commit_id.to_string(),
            commit_summary: msg.into(),
        });
        return true;
    });
    if res.is_err() {
        return Err(res.err().unwrap());
    }
    println!("{}", serde_json::to_string(&info_list).unwrap());
    Ok(())
}
